package com.fingard.dsp.bank.directbank.gzdsf01;

import java.io.File;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IAuthorizeForRec;
import com.fingard.dsp.bank.reqrespobj.authforrec.ReqAuthForRec;
import com.fingard.dsp.bank.reqrespobj.authforrec.ReqQryAuthForRec;
import com.fingard.dsp.bank.reqrespobj.authforrec.ReqQryRecAuthInfo;
import com.fingard.dsp.bank.reqrespobj.authforrec.RetAtsAuthForRec;
import com.fingard.dsp.bank.reqrespobj.authforrec.RetAtsQryAuthForRec;
import com.fingard.dsp.bank.reqrespobj.authforrec.RetAtsQryRecAuthInfo;
import com.fingard.dsp.bank.reqrespobj.authforrec.detail.RetAtsQryAuthForRecDetail;
import com.fingard.dsp.bank.reqrespobj.contract.ReqContract;
import com.fingard.dsp.bank.reqrespobj.contract.RetAtsContract;
import com.fingard.dsp.bank.reqrespobj.contract.detail.ReqContractDetail;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.xml.XmlTextReader;

/**GZDSF01批量授权*/
public class GZDSF01AuthorizeForRec extends GZDSF01Base implements IAuthorizeForRec{

	public RetAtsAuthForRec d_auth(ReqAuthForRec p_req) throws Exception {
		RetAtsAuthForRec tmpRetAts = new RetAtsAuthForRec(p_req.reqHeader);
		try {
			int succCount = 0;
			//int failCount = 0;
			while (p_req.readRecord()) {
				//复用单笔签约
				GZDSF01Contract contrClass = new GZDSF01Contract();
				contrClass.setAuthForRec(true);
				contrClass.bankFront = this.bankFront;
				contrClass.reqHeader = this.reqHeader;
				contrClass.setSbLog(this.sbLog);
				contrClass.setLogFlushTime(this.logFlushTime);
				ReqContract reqCo = new ReqContract();
				reqCo.reqHeader = this.reqHeader;
				reqCo.contractDetail = new ReqContractDetail();
				reqCo.contractDetail.from(p_req.ownAct, p_req.readedDetail);
				reqCo.contractDetail.contractNo = p_req.authAgNum;
				RetAtsContract resp = contrClass.contract(reqCo);
				
				if("0".equals(resp.retDetail.respCode) && !ConnState.FAIL.equals(resp.retDetail.unifiedCode) && !"EC999".equals(resp.retDetail.unifiedCode)){
					succCount++;
				}//else{
				//	failCount++;
				//}
				
				if(succCount == 0){//前三笔全部失败，则不再继续提交
					tmpRetAts.respCode = "0";
					tmpRetAts.reqSeqID = p_req.reqSeqID;
					tmpRetAts.respInfo = resp.retDetail.respInfo;
					tmpRetAts.retCode = "1";
					tmpRetAts.retMsg = resp.retDetail.respInfo;
					//if(failCount>=3){
					//	break;
					//}
				}
			}
			if(succCount > 0){
				tmpRetAts.respCode = "0";
				tmpRetAts.reqSeqID = p_req.reqSeqID;
				tmpRetAts.respInfo = "已提交";
				tmpRetAts.retCode = "0";
				tmpRetAts.retMsg = "已提交";
			}
		} catch (Exception ex) {
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        tmpRetAts.writeRetToATS();
        return tmpRetAts;
	}

	public RetAtsQryAuthForRec d_qry_auth(ReqQryAuthForRec p_req) throws Exception {
		RetAtsQryAuthForRec tmpRetAts = new RetAtsQryAuthForRec(p_req.reqHeader);
        tmpRetAts.writeStart();
        tmpRetAts.reqSeqID = p_req.reqSeqID;
        try {
        	Date timeNow = new Date();
            String tmpContractFilePath = getTempFileDirectory(timeNow) + File.separator + "Contract" + File.separator + Format.Date10Format.format(timeNow) + ".txt";
            if(FileHelper.existFile(tmpContractFilePath)){
            	tmpRetAts.respCode = "0";
            	String resultXml = "<r>"+FileHelper.readFileText(tmpContractFilePath, getCharset())+"</r>";
            	XmlTextReader xmlReader = new XmlTextReader(resultXml);
            	while(xmlReader.read("/r/DetailRecord")){
            		RetAtsQryAuthForRecDetail tmpDetail = new RetAtsQryAuthForRecDetail();
            		if("fail".equals(xmlReader.readedXElement.getSingleText("SignState"))){
            			tmpDetail.transState = "3";
            		}else if("success".equals(xmlReader.readedXElement.getSingleText("SignState"))){
            			tmpDetail.transState = "2";
            		}else{
            			tmpDetail.transState = "4";
            		}
            		tmpDetail.authCode = xmlReader.readedXElement.getSingleText("ReqSeqID");
            		tmpDetail.contractNo = tmpDetail.authCode;
            		tmpDetail.iSeqno = tmpDetail.authCode;
            		tmpDetail.payCode = xmlReader.readedXElement.getSingleText("UnifiedCode");
            		tmpDetail.payInfo = xmlReader.readedXElement.getSingleText("RespInfo");
            		tmpRetAts.writeRetDetail(tmpDetail);
            	}
            	
            }else{
            	tmpRetAts.respCode = "-1";
                tmpRetAts.respInfo = "无结果文件，状态未返回";
            }
        } catch (Exception ex) {
            tmpRetAts.respCode = "-1";
            tmpRetAts.respInfo = ex.getMessage();
            this.WriteBankLogLn(ex);
        }
        if (tmpRetAts.getWriteRdCount() <= 0) {
            tmpRetAts.writeRetHead();
        }
        tmpRetAts.writeAfterDetail();
        return tmpRetAts;
	}

	public RetAtsQryRecAuthInfo d_qry_authInfo(ReqQryRecAuthInfo p_req) throws Exception {
		return null;
	}

}
